<?php

    /**
     * @package      Modules
     * @subpackage   Base_Classes
     *
     * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
     */
     class Base_Classes_Image
     {

	////////////////////////////////////////////////////////////////////////////
	//                          Attributes                                    //
	////////////////////////////////////////////////////////////////////////////

        /**
         * Image's path
         *
         * @var string
         */
        private $_imagePath         = null;

        /**
         * Image's URL
         *
         * @var string
         */
        private $_imageUrl          = null;

        /**
         * Default's image URL
         * 
         * @var string
         */
        private $_defaultUrl        = null;

	////////////////////////////////////////////////////////////////////////////
	//                               Methods                                  //
	////////////////////////////////////////////////////////////////////////////

        /**
         * Constructor
         *
         * @param       array           $extraPath
         * @param       string|null     $defaultImageName
         */
        public function __construct($extraPath, $defaultImageName = null)
        {
            $completePath = Core_IO_Path::combine(
                                                    PROJECT_MEDIA_PATH,
                                                    'modules'
                                                 );

            $registry      = Zend_Registry::getInstance();
            $baseUrl       = $registry['config']->urlengine->basePath .'media/modules';

            foreach ($extraPath as $extraDir) {
                $completePath .= DIRECTORY_SEPARATOR . $extraDir;
                $baseUrl      .= '/'. $extraDir;
            }

            if (Core_IO_File::exists($completePath)) {
                $this->_imagePath   = $completePath;
                $this->_imageUrl    = $baseUrl;

            } elseif ($defaultImageName !== null) {
                $baseUrl     = $registry['config']->urlengine->basePath .'skin/frontend/images/default/' . $defaultImageName;
                $defaultPath = Core_IO_Path::combine(
                                                        WWW_PATH,
                                                        'skin',
                                                        'frontend',
                                                        'images',
                                                        'default',
                                                        $defaultImageName
                                                     );

                if (!Core_IO_File::exists($defaultPath)) {
                    throw new Core_Exception('Fatal error : file "'. $defaultPath .'" not found');
                }

                $this->_defaultUrl = $baseUrl;

            } else {
                throw new Core_Exception('Fatal error : file "'. $completePath .'" not found');
            }
        }

        /**
         * __toString override
         *
         * @return      string
         */
        public function __toString()
        {
            return $this->getUrl();
        }

        /**
         * Returns URL
         *
         * @return      string
         */
        public function getUrl()
        {
            return ($this->_imageUrl !== null) ? $this->_imageUrl : $this->_defaultUrl;
        }
    }
